/**

 @Name：layuiAdmin 内容系统
 @Author：star1029

 @License：LPPL

 */


layui.define(['table','tableTreeDj', 'form'], function(exports){
  var $ = layui.$
      , tableTree = layui.tableTreeDj
      , table = layui.table
      , form = layui.form;

  //文章管理
  tableTree.render({
    elem: '#LAY-app-content-list'
    , escape: false
    , url: CATEGORY_LIST_URL
    , cols: [[
      {type: 'checkbox', fixed: 'left'}
      , {field: 'id', width: 90, title: 'ID'}
      , {field: 'sort', title: '排序', width: 115,templet: '#sortTpl'}
      , {field: 'type_name', title: '类型', width: 30, align: 'center'}
      , {field: 'title', title: '名称', minWidth: 100}
      , {field: 'status', title: '导航', templet: '#buttonTpl', width: 70, align: 'center'}
      , {field: 'is_redirect_child', title: '跳转子栏目', width: 100,templet: '#isRedirectChildTpl', align: 'center'}
      , {field: 'module_name', title: '模型', width: 150}
      , {title: '操作', width: 215, align: 'center', fixed: 'right', toolbar: '#table-content-list'}
    ]]
    , page: false
    , text: '对不起，加载出现异常！'
    ,id:'LAY-app-content-list'
  },{
    keyId: "id" // 当前ID
    , keyPid: "pid" // 上级ID
    , title: "title" // 标题名称字段,此字段td用于绑定单击折叠展开功能
    , indent: ' &nbsp; &nbsp;' // 子级td的缩进.可以是其他字符
    // 图标
    , icon: {
      open: 'layui-icon layui-icon-subtraction layui-tree-icon', // 展开时候图标
      close: 'layui-icon layui-icon-addition layui-tree-icon', // 折叠时候图标
    }
    , showCache: true //是否开启展开折叠缓存,默认不开启.
    , cacheName:WEBSITE_CODE+'_unfoldStatus',
  });

  //监听工具条
  tableTree.getTable().on('tool(LAY-app-content-list)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.confirm('确定删除此栏目？', function(index){
        var field = data.field; //获取提交的字段
        $.ajax({
          method: 'post',
          data: {"id": data.id},
          url: CATEGORY_DELETE_URL,
          success: function (res) {
            if (res.result.status) {
              layer.msg(res.result.msg, {
                offset: '15px'
                , icon: 2
                , time: 1000
              });
              return
            }
            obj.del();
            layui.table.reload('LAY-app-content-list'); //重载表格
            form.render();
            layer.close(index);
          }
        })
      });
    } else if(obj.event === 'edit'){
      layer.open({
        type: 2
        ,title: '编辑栏目'
        ,content: CATEGORY_EDIT_URL + '&id=' + data.id
        ,maxmin: true
        ,area: ['850px', '720px']
        ,btn: ['确定', '取消']
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
          ,submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");

          //监听提交
          iframeWindow.layui.form.on('submit(layuiadmin-app-form-edit)', function(data){
            var field = data.field; //获取提交的字段

            //提交 Ajax 成功后，静态更新表格中的数据
            $.ajax({
              method: 'post',
              data: field,
              url: CATEGORY_DO_EDIT_URL,
              success: function (res) {
                if (res.result.status) {
                  layer.msg(res.result.msg, {
                    offset: '15px'
                    , icon: 2
                    , time: 1000
                  });
                  return
                }

                layer.msg(res.result.msg, {
                  offset: '15px'
                  , icon: 1
                  , time: 1000
                });

                layui.table.reload('LAY-app-content-list'); //重载表格
                form.render();
                layer.close(index); //关闭弹层
              }
            });

          });

          submit.trigger('click');
        }
      });
    } else if(obj.event === 'add-child'){
      layer.open({
        type: 2
        ,title: '新增栏目'
        ,content: CATEGORY_ADD_URL+'&pid='+data.id
        ,maxmin: true
        ,area: ['750px', '700px']
        ,btn: ['确定', '取消']
        ,yes: function(index, layero){
          //点击确认触发 iframe 内容中的按钮提交
          var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
          submit.click();
        }
      });
    }
  });


  exports('category', {})
});